package com.hxzy.tms.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.hxzy.tms.dao.TaskDao;
import com.hxzy.tms.dao.TeamDao;
import com.hxzy.tms.entity.Task;
import com.hxzy.tms.entity.Team;
import com.hxzy.tms.entity.User;
import com.hxzy.tms.service.ServiceResult;
import com.hxzy.tms.service.TaskService;

public class DefaultTaskService implements TaskService{
    private  TaskDao taskDao;
    private TeamDao teamDao;
    private List<Team> teams;
	public ServiceResult<Task> createTask(String title, String desct, String attachmenturl, Long gmtstart,Long gmtfinished,User user) {
		
		  Map<String,Object> param =new HashMap<String ,Object>();
		    param.put("uid",user.getId() );
		  teams=teamDao.read("from Team where  userCreate=:uid", param);
		/*  for( Serializable  team : teams){
			 Long id=((Team) team).getUserCreate().getId(); 
			  if(user.getId()!=id){
				  return new ServiceResult<Task>(-1, null,"您不是该团队的创建者，无权对其发布任务");
					
			   }
		  }
	   */
	     param=new HashMap<String, Object>();
		 param.put("title", title);
		 Task task=(Task )taskDao.read("from Task where title=:", param );
		 if(task!=null){
			 return new ServiceResult<Task>(-2, null,"存在相同的任务名");
		 }
		 if(gmtstart>=gmtfinished){
			 return new ServiceResult<Task>(-3, null,"任务开始时间不能大于或等于任务结束时间"); 
		 }
		 Task task1=new Task();
		 task1.setDesct(desct);
		 task1.setGmtstart(gmtstart);
		 task1.setGmtfinished(gmtfinished);
		 task1.setTeams(teams);
		 task1.setUidcreate(user);
		 task1.setUidmodified(user);
		 task1.setAttachmenturl(attachmenturl);
		 taskDao.create(task1);
		 return new ServiceResult<Task>(1, null,"发布任务成功"); 
	}
	public TaskDao getTaskDao() {
		return taskDao;
	}
	public void setTaskDao(TaskDao taskDao) {
		this.taskDao = taskDao;
	}
	public TeamDao getTeamDao() {
		return teamDao;
	}
	public void setTeamDao(TeamDao teamDao) {
		this.teamDao = teamDao;
	}
	public List<Team> getTeams() {
		return teams;
	}
	public void setTeams(List<Team> teams) {
		this.teams = teams;
	}
}
